/**
 * 
 */
package com.kathez.util.commonutils;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.InvalidPropertiesFormatException;
import java.util.Map;
import java.util.Properties;

/**
 * @author Balaji T
 *
 */
public class PropertyLoader {
	 
	private static final Properties propertyFilesMap = new Properties();
	private static final Map<String,Properties> propertiesMap = new HashMap<String,Properties>();
	static{
		try{
			propertyFilesMap.loadFromXML(new FileInputStream("propertyfiles.properties"));
			for(Object prop : propertyFilesMap.keySet()){
				Properties property = new Properties();
				property.loadFromXML(new FileInputStream(propertyFilesMap.getProperty((String)prop)));
				propertiesMap.put((String)prop, property);
			}
		}catch(FileNotFoundException fileNotFoundExp){
			System.err.println(fileNotFoundExp.getMessage());
		} catch (InvalidPropertiesFormatException invalidPropsExp) {
			System.err.println(invalidPropsExp.getMessage());
		} catch (IOException ioExecp) {
			System.err.println(ioExecp.getMessage());
		}
		
	}
	
	public static String getProperty(String propFileName, String key){
		String value = null;
		if(propFileName == null || key == null || propertiesMap == null)
			return null;
		Properties props = propertiesMap.get(propFileName);
		if(props != null)
			value = props.getProperty(key);
		return value;
	}
	
	public static String getProperty(String key){
		String value = null; 
		if(key == null || propertiesMap == null)
			return null;
		for(String propFileName : propertiesMap.keySet()){
			value = getProperty(propFileName,key);
			if(value != null)
				break;
		}
		return value;
	}

}
